#include <vector>
#include <iostream>

using namespace std;

struct ListNode
{
    int val;
    ListNode * next;
    ListNode() : val(0), next(nullptr)
    {}
    ListNode(int val) : val(val), next(nullptr)
    {}
    ListNode(int val, ListNode * next) : val(val), next(next)
    {}
};

class Solution 
{
public:
    ListNode* swapPairs(ListNode* head) 
    {
        ListNode * _dummyHead = new ListNode(0, head);
        ListNode * cur1 = _dummyHead;
        ListNode * cur2 = _dummyHead->next;
        while(cur2 != nullptr && cur2->next != nullptr)
        {
            cur1->next = cur1->next->next;
            cur2->next = cur2->next->next;
            cur1->next->next = cur2;
            cur1 = cur2;
            cur2 = cur2->next;
        }
        ListNode * result = _dummyHead->next;
        delete _dummyHead;
        return result;
    }
};

// 辅助函数：从vector创建链表
ListNode* createList(vector<int>& nums) {
    if (nums.empty()) return nullptr;
    
    ListNode* head = new ListNode(nums[0]);
    ListNode* cur = head;
    
    for (int i = 1; i < nums.size(); i++) {
        cur->next = new ListNode(nums[i]);
        cur = cur->next;
    }
    
    return head;
}

// 辅助函数：打印链表
void printList(ListNode* head) {
    ListNode* cur = head;
    cout << "[";
    while (cur != nullptr) {
        cout << cur->val;
        if (cur->next != nullptr) cout << ",";
        cur = cur->next;
    }
    cout << "]";
}

// 辅助函数：释放链表内存
void deleteList(ListNode* head) {
    while (head != nullptr) {
        ListNode* temp = head;
        head = head->next;
        delete temp;
    }
}

int main() {
    Solution solution;
    
    // 测试案例1：题目示例1 [1,2,3,4]
    cout << "测试案例1: ";
    vector<int> nums1 = {1, 2, 3, 4};
    ListNode* head1 = createList(nums1);
    cout << "head = ";
    printList(head1);
    ListNode* result1 = solution.swapPairs(head1);
    cout << " -> ";
    printList(result1);
    cout << " (期望: [2,1,4,3])" << endl;
    deleteList(result1);
    
    // 测试案例2：题目示例2 []
    cout << "测试案例2: ";
    vector<int> nums2 = {};
    ListNode* head2 = createList(nums2);
    cout << "head = ";
    printList(head2);
    ListNode* result2 = solution.swapPairs(head2);
    cout << " -> ";
    printList(result2);
    cout << " (期望: [])" << endl;
    deleteList(result2);
    
    // 测试案例3：题目示例3 [1]
    cout << "测试案例3: ";
    vector<int> nums3 = {1};
    ListNode* head3 = createList(nums3);
    cout << "head = ";
    printList(head3);
    ListNode* result3 = solution.swapPairs(head3);
    cout << " -> ";
    printList(result3);
    cout << " (期望: [1])" << endl;
    deleteList(result3);
    
    // 测试案例4：两个节点 [1,2]
    cout << "测试案例4: ";
    vector<int> nums4 = {1, 2};
    ListNode* head4 = createList(nums4);
    cout << "head = ";
    printList(head4);
    ListNode* result4 = solution.swapPairs(head4);
    cout << " -> ";
    printList(result4);
    cout << " (期望: [2,1])" << endl;
    deleteList(result4);
    
    // 测试案例5：三个节点 [1,2,3]
    cout << "测试案例5: ";
    vector<int> nums5 = {1, 2, 3};
    ListNode* head5 = createList(nums5);
    cout << "head = ";
    printList(head5);
    ListNode* result5 = solution.swapPairs(head5);
    cout << " -> ";
    printList(result5);
    cout << " (期望: [2,1,3])" << endl;
    deleteList(result5);
    
    // 测试案例6：五个节点 [1,2,3,4,5]
    cout << "测试案例6: ";
    vector<int> nums6 = {1, 2, 3, 4, 5};
    ListNode* head6 = createList(nums6);
    cout << "head = ";
    printList(head6);
    ListNode* result6 = solution.swapPairs(head6);
    cout << " -> ";
    printList(result6);
    cout << " (期望: [2,1,4,3,5])" << endl;
    deleteList(result6);
    
    // 测试案例7：六个节点 [1,2,3,4,5,6]
    cout << "测试案例7: ";
    vector<int> nums7 = {1, 2, 3, 4, 5, 6};
    ListNode* head7 = createList(nums7);
    cout << "head = ";
    printList(head7);
    ListNode* result7 = solution.swapPairs(head7);
    cout << " -> ";
    printList(result7);
    cout << " (期望: [2,1,4,3,6,5])" << endl;
    deleteList(result7);
    
    return 0;
}